package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //http://localhost:8080/category/list?type=1

    @GetMapping("/list")
    public ResultInfo findByType(Integer type){
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,type);
        List<Category> categoryList = categoryService.list(wrapper);
        return ResultInfo.success(categoryList);
    }
    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping("")
    public ResultInfo deleteById(long id){
        return categoryService.deleteById(id);
    }
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("")
    public ResultInfo save(@RequestBody Category category){
        categoryService.save1(category);
        return ResultInfo.success(null);
    }
    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping("")
    public ResultInfo update(@RequestBody Category category){
        categoryService.update(category);
        return ResultInfo.success(null);
    }
    @GetMapping("/findAll")
    public ResultInfo findAll(){
        List<Category> categoryList= categoryService.findAll();
        return ResultInfo.success(categoryList);
    }
    //http://localhost:8080/category/findAll
}
